// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   StatusReportMessage.java

package cn.sendsms;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

// Referenced classes of package cn.sendsms:
//			InboundMessage, MessageTypes, DeliveryStatuses

public class StatusReportMessage extends InboundMessage
{

	private String recipient;
	private Date sent;
	private Date received;
	private DeliveryStatuses status;
	private String refNo;

	StatusReportMessage(String pdu, int memIndex, String memLocation)
	{
		super(MessageTypes.STATUSREPORT, memIndex, memLocation);
		int i = Integer.parseInt(pdu.substring(0, 2), 16);
		int index = (i + 1) * 2;
		index += 2;
		refNo = "" + Integer.parseInt(pdu.substring(index, index + 2), 16);
		index += 2;
		i = Integer.parseInt(pdu.substring(index, index + 2), 16);
		int j = index + 4;
		String recipient = "";
		for (int k = 0; k < i; k += 2)
			recipient = recipient + pdu.charAt(j + k + 1) + pdu.charAt(j + k);

		recipient = "+" + recipient;
		if (recipient.charAt(recipient.length() - 1) == 'F')
		{
			recipient = recipient.substring(0, recipient.length() - 1);
			i++;
		}
		this.recipient = recipient;
		index = j + i;
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
		String dateOctect = pdu.substring(index, index + 12);
		StringBuffer dateOk = new StringBuffer();
		for (int x = 0; x < 12; x += 2)
			dateOk.append(new char[] {
				dateOctect.charAt(x + 1), dateOctect.charAt(x)
			});

		try
		{
			sent = sdf.parse(dateOk.toString());
		}
		catch (ParseException e)
		{
			sent = null;
		}
		index += 14;
		dateOctect = pdu.substring(index, index + 12);
		dateOk = new StringBuffer();
		for (int x = 0; x < 12; x += 2)
			dateOk.append(new char[] {
				dateOctect.charAt(x + 1), dateOctect.charAt(x)
			});

		try
		{
			received = sdf.parse(dateOk.toString());
		}
		catch (ParseException e)
		{
			received = null;
		}
		index += 14;
		i = Integer.parseInt(pdu.substring(index, index + 2), 16);
		if ((i & 0x60) == 0)
		{
			setText("00 - Succesful Delivery.");
			status = DeliveryStatuses.DELIVERED;
		}
		if ((i & 0x20) == 32)
		{
			setText("01 - Errors, will retry dispatch.");
			status = DeliveryStatuses.KEEPTRYING;
		}
		if ((i & 0x40) == 64)
		{
			setText("02 - Errors, stopped retrying dispatch.");
			status = DeliveryStatuses.ABORTED;
		}
		if ((i & 0x60) == 96)
		{
			setText("03 - Errors, stopped retrying dispatch.");
			status = DeliveryStatuses.ABORTED;
		}
		setDate(null);
	}

	StatusReportMessage(String refNo, int memIndex, String memLocation, Date dateOriginal, Date dateReceived)
	{
		super(MessageTypes.STATUSREPORT, memIndex, memLocation);
		this.refNo = refNo;
		sent = dateOriginal;
		received = dateReceived;
		setText("");
		status = DeliveryStatuses.UNKNOWN;
		setDate(null);
	}

	public String getRecipient()
	{
		return recipient;
	}

	public Date getReceived()
	{
		return new Date(received.getTime());
	}

	void setReceived(Date received)
	{
		this.received = received;
	}

	public Date getSent()
	{
		return new Date(sent.getTime());
	}

	void setSent(Date sent)
	{
		this.sent = sent;
	}

	public DeliveryStatuses getStatus()
	{
		return status;
	}

	void setStatus(DeliveryStatuses status)
	{
		this.status = status;
	}

	public String getRefNo()
	{
		return refNo;
	}
}
